package com.xiaohong.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohong.user.pojo.UserAddress;
import com.xiaohong.user.service.UserAddressService;
import entity.Result;
import entity.StatusCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "UserAddress接口文档")
@RestController
@RequestMapping("/userAddress")
@CrossOrigin
public class UserAddressController {

    @Autowired
    private UserAddressService userAddressService;

    /***
     * UserAddress分页条件搜索实现
     * @param userAddress
     * @param page
     * @param size
     * @return
     */
    @Operation(summary = "UserAddress条件分页查询",description = "所需权限：")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@RequestBody(required = false) @Parameter(name = "UserAddress对象",required = true) UserAddress userAddress, @PathVariable  int page, @PathVariable  int size){
        //调用UserAddressService实现分页条件查询UserAddress
        Page pageInfo = userAddressService.findPage(userAddress, page, size);
        return new Result<Page>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * UserAddress分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @Operation(summary = "UserAddress分页查询",description = "所需权限：")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用UserAddressService实现分页查询UserAddress
        Page pageInfo = userAddressService.findPage(page, size);
        return new Result<Page>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索数据
     * @param userAddress
     * @return
     */
    @Operation(summary = "UserAddress条件查询",description = "所需权限：")
    @PostMapping(value = "/search" )
    public Result<List<UserAddress>> findList(@RequestBody(required = false) @Parameter(name = "UserAddress对象",required = true) UserAddress userAddress){
        //调用UserAddressService实现条件查询UserAddress
        List<UserAddress> list = userAddressService.findList(userAddress);
        return new Result<List<UserAddress>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据Id删除userAddress数据
     * @param id
     * @return
     */
    @Operation(summary = "UserAddress根据ID删除",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "主键ID", required = true)
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Long id){
        //调用UserAddressService实现根据主键删除
        userAddressService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 修改UserAddress数据
     * @param userAddress
     * @param id
     * @return
     */
    @Operation(summary = "UserAddress根据ID修改",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "主键ID", required = true)
    @PutMapping(value="/{id}")
    public Result update(@RequestBody @Parameter(name = "UserAddress对象",required = true) UserAddress userAddress,@PathVariable Long id){
        //设置主键值
        userAddress.setId(id);
        //调用UserAddressService实现修改UserAddress
        userAddressService.update(userAddress);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增UserAddress数据
     * @param userAddress
     * @return
     */
    @Operation(summary = "UserAddress添加",description = "所需权限：")
    @PostMapping
    public Result add(@RequestBody  @Parameter(name = "UserAddress对象",required = true) UserAddress userAddress){
        //调用UserAddressService实现添加UserAddress
        userAddressService.add(userAddress);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据主键查询UserAddress数据
     * @param id
     * @return
     */
    @Operation(summary = "UserAddress根据ID查询",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "主键ID", required = true)
    @GetMapping("/{id}")
    public Result<UserAddress> findById(@PathVariable Long id){
        //调用UserAddressService实现根据主键查询UserAddress
        UserAddress userAddress = userAddressService.findById(id);
        return new Result<UserAddress>(true,StatusCode.OK,"查询成功",userAddress);
    }

    /***
     * 查询UserAddress全部数据
     * @return
     */
    @Operation(summary = "查询所有UserAddress",description = "所需权限：")
    @GetMapping
    public Result<List<UserAddress>> findAll(){
        //调用UserAddressService实现查询所有UserAddress
        List<UserAddress> list = userAddressService.findAll();
        return new Result<List<UserAddress>>(true, StatusCode.OK,"查询成功",list) ;
    }

}
